import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Stadium implements Stadium_var{
	boolean[][] state;

	int counter = 0;

	JTable seat = new JTable();
	JPanel ns_panel = new JPanel();
	JTable ns = new JTable();
	JPanel es_panel = new JPanel();
	JTable es = new JTable();
	JPanel ss_panel = new JPanel();
	JTable ss = new JTable();
	JPanel ws_panel = new JPanel();
	JTable ws = new JTable();
	String direction;
	String currentMatch;
	String currentID = null;
	int noOfPeople;
	int temp;
	
//For the quick seat
	public void random(int sizeOfX, int sizeOfY, JTable directionSeat) {
		String line = " ";	//to show the reserved seats

		for (int i = 0; i < 2000; i++) {
			temp = 1;
			int X = (int) (Math.random() * sizeOfX);
			int Y = (int) (Math.random() * (sizeOfY - noOfPeople + 1));
			for (int a = 0; a < noOfPeople; a++) {
				if (state[X][Y + a] == true
						|| directionSeat.getValueAt(X, Y + a).equals("x")) {
					temp = temp * 0;
				}
			}
			if (temp != 0) {
				for (int b = 0; b < noOfPeople; b++) {
					state[X][Y + b] = true;
					line = line + directionSeat.getValueAt(X, Y + b) + "  ";
				}
				break;
			}
		}
		if (temp == 1)
			JOptionPane.showMessageDialog(null, line + "\nare reservated");

		if (temp == 0) {
			JOptionPane.showMessageDialog(null, "NO Seats Available");

		}
	}

//read currentID.txt
	public void currentIDReader() throws IOException {
		FileReader currentIDReader = new FileReader("currentID.txt");
		BufferedReader currentIDBufferedReader = new BufferedReader(
				currentIDReader);

		String line = null;
		while ((line = currentIDBufferedReader.readLine()) != null) {
			StringTokenizer st = new StringTokenizer(line);
			currentID = st.nextToken();
		}
		currentIDReader.close();
		currentIDBufferedReader.close();
	}

	//it read Match_diretion.txt and currentID.txt and write the new reserved seats
	// readwriteOnMatchNID(1,North,ns,Suwon-VS-Seoul_)
	public void readwriteOnMatchNID(int matchSceNo, String direction,
			JTable directionSeat, String currentMatch) {

		for (int i = 0; i < state.length; i++) {
			for (int j = 0; j < state[i].length; j++) {
				if (state[i][j] == true) {
					try {
						TextManager reservedSeatmanager = new TextManager(
								"MatchSchedule" + matchSceNo + "_" + direction);
						ArrayList<String> reservedSeat = new ArrayList<String>();
						reservedSeat = reservedSeatmanager.getTextList();

						reservedSeat.add((String) directionSeat
								.getValueAt(i, j));

						reservedSeatmanager.setTextList(reservedSeat);

						reservedSeatmanager.saveText("MatchSchedule"
								+ matchSceNo + "_" + direction);
						// -------------------------------------------------------------------------
						currentIDReader();
						// -----------------------------------------------------------------------------------
						TextManager IDInfomanager = new TextManager(currentID);
						ArrayList<String> IDInfoList = new ArrayList<String>();
						IDInfoList = IDInfomanager.getTextList();

						IDInfoList.add(currentMatch
								+ (String) directionSeat.getValueAt(i, j));

						IDInfomanager.setTextList(IDInfoList);

						IDInfomanager.saveText(currentID);

					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// ----------------

				}
			}

		}
	}

	// readwriteOnMatchNID(int matchSceNo,String direction,JTable
	// directionSeat,String currentMatch)
	public void quickSeat(String currentMatch, String direction, int noOfPeople) {
		this.currentMatch = currentMatch;
		this.direction = direction;
		this.noOfPeople = noOfPeople;
		// Match1
		if (currentMatch.equals("Suwon vs Seoul      12/14    12:00")) {
			if (direction.equals("A class Seat (West)")) {
				// set the random seat true
				random(4, 10, ws);
				readwriteOnMatchNID(1, "West", ws, "Suwon-VS-Seoul_");
			}
			if (direction.equals("B class Seat (East)")) {
				random(4, 10, es);
				readwriteOnMatchNID(1, "East", es, "Suwon-VS-Seoul_");
			}
			if (direction.equals("C class Seat (North)")) {
				random(4, 10, ns);
				readwriteOnMatchNID(1, "North", ns, "Suwon-VS-Seoul_");
			}
			if (direction.equals("D class Seat (South)")) {
				random(4, 10, ss);
				readwriteOnMatchNID(1, "South", ss, "Suwon-VS-Seoul_");
			}
		}
		// Match 2
		if (currentMatch.equals("Busan vs Pohang     12/16    15:00")) {
			if (direction.equals("A class Seat (West)")) {
				// set the random seat true
				random(4, 9, ws);
				readwriteOnMatchNID(2, "West", ws, "Busan-VS-Pohang_");
			}
			if (direction.equals("B class Seat (East)")) {
				random(4, 9, es);
				readwriteOnMatchNID(2, "East", es, "Busan-VS-Pohang_");
			}
			if (direction.equals("C class Seat (North)")) {
				random(4, 9, ns);
				readwriteOnMatchNID(2, "North", ns, "Busan-VS-Pohang_");
			}
			if (direction.equals("D class Seat (South)")) {
				random(4, 9, ss);
				readwriteOnMatchNID(2, "South", ss, "Busan-VS-Pohang_");
			}
		}
		// Match 3
		if (currentMatch.equals("Suwon vs Busan      12/18    13:00")) {
			if (direction.equals("A class Seat (West)")) {
				random(3, 12, ws);
				readwriteOnMatchNID(3, "West", ws, "Suwon-VS-Busan_");
			}
			if (direction.equals("B class Seat (East)")) {
				random(3, 12, es);
				readwriteOnMatchNID(3, "East", es, "Suwon-VS-Busan_");
			}
			if (direction.equals("C class Seat (North)")) {
				random(3, 12, ns);
				readwriteOnMatchNID(3, "North", ns, "Suwon-VS-Busan_");
			}
			if (direction.equals("D class Seat (South)")) {
				random(3, 12, ss);
				readwriteOnMatchNID(3, "South", ss, "Suwon-VS-Busan_");
			}
		}
	}
//help to find the match
	public void findMatch(String currentMatch, String direction) {
		this.currentMatch = currentMatch;
		this.direction = direction;
		// MATCH 1
		if (currentMatch.equals(" Suwon vs Seoul      12/14    12:00")) {
			// 1-North
			if (direction.equals("C class Seat (North)")) {
				readwriteOnMatchNID(1, "North", ns, "Suwon-VS-Seoul_");
				n_seat();
			}
			// 1-South
			else if (direction.equals("D class Seat (South)")) {
				readwriteOnMatchNID(1, "South", ss, "Suwon-VS-Seoul_");
				s_seat();
			}
			// 1-East
			else if (direction.contains("B class Seat (East)")) {
				readwriteOnMatchNID(1, "East", es, "Suwon-VS-Seoul_");
				e_seat();
			}
			// 1-West
			else if (direction.contains("A class Seat (West)")) {
				readwriteOnMatchNID(1, "West", ws, "Suwon-VS-Seoul_");
				w_seat();
			}
			// Match 2 Seoul
		} else if (currentMatch.equals(" Busan vs Pohang     12/16    15:00")) {
			// 2-North
			if (direction.equals("C class Seat (North)")) {
				readwriteOnMatchNID(2, "North", ns, "Busan-VS-Pohang_");
				n_seat();
			}
			// 2-South
			else if (direction.equals("D class Seat (South)")) {
				readwriteOnMatchNID(2, "South", ss, "Busan-VS-Pohang_");
				s_seat();
			}
			// /Match 2 East
			else if (direction.contains("B class Seat (East)")) {
				readwriteOnMatchNID(2, "East", es, "Busan-VS-Pohang_");
				e_seat();
			}
			// /Match2 West
			else if (direction.contains("A class Seat (West)")) {
				readwriteOnMatchNID(2, "West", ws, "Busan-VS-Pohang_");
				w_seat();
			}
			// Match2 End
		}
		// Match3
		else if (currentMatch.equals(" Suwon vs Busan      12/18    13:00")) {
			// Match3 North
			if (direction.equals("C class Seat (North)")) {
				readwriteOnMatchNID(3, "North", ns, "Suwon-VS-Busan_");
				n_seat();
			}
			// Match3 South
			else if (direction.equals("D class Seat (South)")) {
				readwriteOnMatchNID(3, "South", ss, "Suwon-VS-Busan_");
				s_seat();
			}
			// Match3 East
			else if (direction.contains("B class Seat (East)")) {
				readwriteOnMatchNID(3, "East", es, "Suwon-VS-Busan_");
				e_seat();
			}
			// Match3 West
			else if (direction.contains("A class Seat (West)")) {
				readwriteOnMatchNID(3, "West", ws, "Suwon-VS-Busan_");
				w_seat();
			}
		}
		counter = 0;
		JOptionPane.showMessageDialog(null, "Ticketing Done!");

	}

	//empty north seat
	public JPanel n_seat() {
		go(ns);
		ns_panel.setSize(470, 110);
		ns_panel.setLayout(null);
		ns.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		ns.setCellSelectionEnabled(true);

		ns.setBounds(12, 10, 450, 64);
		ns.setBackground(Color.WHITE);
		ns.setForeground(Color.BLACK);
		ns.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] {}));

		ns_panel.add(ns);

		return ns_panel;

	}

	//empty south seat
	public JPanel s_seat() {
		go(ss);
		ss_panel.setSize(470, 110);
		ss_panel.setLayout(null);
		ss.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		ss.setCellSelectionEnabled(true);

		ss.setBounds(12, 10, 450, 64);
		ss.setBackground(Color.WHITE);
		ss.setForeground(Color.BLACK);
		ss.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] {}));

		ss_panel.add(ss);

		return ss_panel;

	}
	//empty east seat
	public JPanel e_seat() {
		go(es);
		es_panel.setSize(470, 110);
		es_panel.setLayout(null);
		es.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		es.setCellSelectionEnabled(true);

		es.setBounds(12, 10, 450, 64);
		es.setBackground(Color.WHITE);
		es.setForeground(Color.BLACK);
		es.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] {}));

		es_panel.add(es);

		return es_panel;

	}
	//empty west seat
	public JPanel w_seat() {
		go(ws);
		ws_panel.setSize(470, 110);
		ws_panel.setLayout(null);
		ws.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		ws.setCellSelectionEnabled(true);

		ws.setBounds(12, 10, 450, 64);
		ws.setBackground(Color.WHITE);
		ws.setForeground(Color.BLACK);
		ws.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] {}));

		ws_panel.add(ws);

		return ss_panel;

	}
	//Add Renderer and Mouseevent
	public void go(final JTable seat) {
		class MyRenderer extends DefaultTableCellRenderer {
			public Component getTableCellRendererComponent(JTable table,
					Object value, boolean isSelected, boolean hasFocus,
					int row, int col) {
				Component c = super.getTableCellRendererComponent(table, value,
						isSelected, hasFocus, row, col);
				if (state[row][col] && seat.getValueAt(row, col) != "x")
					c.setBackground(Color.GREEN);
				else
					c.setBackground(table.getBackground());

				return c;
			}
		}

		seat.setModel(new DefaultTableModel(new Object[][] {

		}, new String[] {}));
		seat.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int NoOfPeople = 0;

				ArrayList<Integer> peoplenumber = new ArrayList<Integer>();

				try {
					FileReader numberReader = new FileReader("peoplenumber.txt");
					BufferedReader numberBufferedReader = new BufferedReader(
							numberReader);

					String line = null;
					while ((line = numberBufferedReader.readLine()) != null) {
						StringTokenizer st = new StringTokenizer(line);
						String a = st.nextToken();

						NoOfPeople = Integer.parseInt(a);
					}
				} catch (IOException e) {

				}
				int row = seat.getSelectedRow();
				int col = seat.getSelectedColumn();

				seat.setSelectionBackground(Color.green);
				try{
				if (seat.getValueAt(row, col) == "x") {
					JOptionPane.showMessageDialog(null, "Reserved Seat!!");
				} else if (state[row][col]) {
					state[row][col] = false;
					counter--;
				} else {
					MyRenderer a = new MyRenderer();

					seat.setDefaultRenderer(Object.class, a);

					counter++;
					if (NoOfPeople < counter) {
						JOptionPane
								.showMessageDialog(null,
										"You cannot choose seat over number of people!");
						counter--;
					} else
						state[row][col] = true;
				}
				}
				catch(ArrayIndexOutOfBoundsException e){
					
				}
			}
		});
	}

	public void setState(JTable table) {
		int rowCount = table.getRowCount();
		int colCount = table.getColumnCount();

		state = new boolean[rowCount][colCount];
	}

	public boolean[][] getState() {
		return state;
	}

}
